home *** CD-ROM | disk | FTP | other *** search
/ Hobby PC 15 / Hobby PC 15.iso / Soft / Blender / blender.exe / ic255.cab / python / objimport.py < prev    next >
Text File  |  2001-03-15  |  6KB  |  173 lines

  1. #######################
  2. # (c) Jan Walter 2000 #
  3. #######################
  4.  
  5. # CVS
  6. # $Author: jan $
  7. # $Date: 2001/02/02 10:57:45 $
  8. # $RCSfile: objimport.py,v $
  9. # $Revision: 1.3 $
  10.  
  11. import string
  12. import os
  13. import Blender
  14.  
  15. def testObjImport(filename):
  16.     print 'testObjImport("%s")' % filename
  17.     vcount  = 0
  18.     vncount = 0
  19.     fcount  = 0
  20.     pointList    = []
  21.     normalList   = []
  22.     faceList     = []
  23.     materialList = []
  24.     file = open(filename, "r")
  25.     lines = file.readlines()
  26.     linenumber = 1
  27.     for line in lines:
  28.         words = string.split(line)
  29.         if words and words[0] == "#":
  30.             pass # ignore comments
  31.         elif words and words[0] == "v":
  32.             vcount = vcount + 1
  33.             x = string.atof(words[1])
  34.             y = string.atof(words[2])
  35.             z = string.atof(words[3])
  36.             pointList.append([x, y, z])
  37.         elif words and words[0] == "vn":
  38.             vncount = vncount + 1
  39.             i = string.atof(words[1])
  40.             j = string.atof(words[2])
  41.             k = string.atof(words[3])
  42.             normalList.append([i, j, k])
  43.         elif words and words[0] == "f":
  44.             fcount = fcount + 1
  45.             vi = [] # vertex  indices
  46.             ti = [] # texture indices
  47.             ni = [] # normal  indices
  48.             words = words[1:]
  49.             lcount = len(words)
  50.             for index in (xrange(lcount)):
  51.                 vtn = string.split(words[index], "/")
  52.                 vi.append(string.atoi(vtn[0]))
  53.                 if len(vtn) > 1 and vtn[1]:
  54.                     ti.append(string.atoi(vtn[1]))
  55.                 if len(vtn) > 2 and vtn[2]:
  56.                     ni.append(string.atoi(vtn[2]))
  57.             faceList.append([vi, ti, ni])
  58.         elif words and words[0] == "mtllib":
  59.             # try to export materials
  60.             directory, dummy = os.path.split(filename)
  61.             filename = os.path.join(directory, words[1])
  62.             try:
  63.                 file = open(filename, "r")
  64.             except:
  65.                 print "no material file %s" % filename
  66.             else:
  67.                 file = open(filename, "r")
  68.                 line = file.readline()
  69.                 while line:
  70.                     words = string.split(line)
  71.                     if words and words[0] == "newmtl":
  72.                         name = words[1]
  73.                         file.readline() # Ns .
  74.                         file.readline() # d .
  75.                         file.readline() # illum .
  76.                         line = file.readline()
  77.                         words = string.split(line)
  78.                         Kd = [string.atof(words[1]),
  79.                               string.atof(words[2]),
  80.                               string.atof(words[3])]
  81.                         line = file.readline()
  82.                         words = string.split(line)
  83.                         Ks = [string.atof(words[1]),
  84.                               string.atof(words[2]),
  85.                               string.atof(words[3])]
  86.                         line = file.readline()
  87.                         words = string.split(line)
  88.                         Ka = [string.atof(words[1]),
  89.                               string.atof(words[2]),
  90.                               string.atof(words[3])]
  91.                         materialList.append([name, Kd, Ks, Ka])
  92.                     line = file.readline()
  93.                 file.close()
  94.         elif words and words[0] == "usemtl":
  95.             name = words[1]
  96.             print "usemtl %s" % name
  97.         elif words:
  98.             ##pass
  99.             print "%s: %s" % (linenumber, words)
  100.         linenumber = linenumber + 1
  101.     file.close()
  102.     # import in Blender
  103.     print "import into Blender ..."
  104.     print "triangles and quads ..."
  105.     scene  = Blender.getCurrentScene()
  106.     mesh   = Blender.Mesh("OBJ")
  107.     object = Blender.Object("OBJ")
  108.     mesh.enterEditMode()
  109.     for face in faceList:
  110.         vi, ti, ni = face
  111. ##         if ti:
  112. ##             print ti
  113.         if len(vi) == 3: # triangle
  114.             indices = []
  115.             for vertex in vi:
  116.                 x, y, z = pointList[vertex-1] # in OBJ the indices start with 1
  117.                 index = mesh.addVertex(x, y, z, 0, 0, 0)
  118.                 indices.append(index)
  119.             mesh.addFace(indices[0], indices[1], indices[2], 0, 0, 0)
  120.         elif len(vi) == 4: # quad
  121.             indices = []
  122.             for vertex in vi:
  123.                 x, y, z = pointList[vertex-1] # in OBJ the indices start with 1
  124.                 index = mesh.addVertex(x, y, z, 0, 0, 0)
  125.                 indices.append(index)
  126.             mesh.addFace(indices[0], indices[1],
  127.                          indices[2], indices[3], 0, 0)
  128. ##         else:
  129. ##             print vi, ti, ni
  130.     Blender.connect(object, mesh)
  131.     Blender.connect(scene, object)
  132.     mesh.leaveEditMode()
  133.     print "all other (general) polygons ..."
  134.     for face in faceList:
  135.         vi, ti, ni = face
  136.         if len(vi) != 3 and len(vi) != 4:
  137.             # export the polygon as edges
  138.             mesh   = Blender.Mesh("OBJ")
  139.             object = Blender.Object("OBJ")
  140.             mesh.enterEditMode()
  141.             for i in xrange(len(vi)-1):
  142.                 x, y, z = pointList[vi[i]-1] # in OBJ the indices start with 1
  143.                 index1 = mesh.addVertex(x, y, z, 0, 0, 0)
  144.                 x, y, z = pointList[vi[i+1]-1] # see above
  145.                 index2 = mesh.addVertex(x, y, z, 0, 0, 0)
  146.                 mesh.addFace(index1, index2, 0, 0, 0, 0)
  147.             # add edge from last point to the first point to close the polygon
  148.             x, y, z = pointList[vi[0]-1] # in OBJ the indices start with 1
  149.             index1 = mesh.addVertex(x, y, z, 0, 0, 0)
  150.             x, y, z = pointList[vi[-1]-1] # in OBJ the indices start with 1
  151.             index2 = mesh.addVertex(x, y, z, 0, 0, 0)
  152.             mesh.addFace(index1, index2, 0, 0, 0, 0)
  153.             # remove doubles and triangulate
  154.             mesh.createTrianglesFromEdges()
  155.             # now connect ...
  156.             Blender.connect(object, mesh)
  157.             Blender.connect(scene, object)
  158.             mesh.leaveEditMode()
  159.     print "... finished"
  160.  
  161. def callback(fs):
  162.     filename = fs.filename
  163.     testObjImport(filename)
  164.  
  165. if __name__ == "__main__":
  166.     try:
  167.         import GUI
  168.     except:
  169.         print "This script is only working with the new GUI module ..."
  170.     else:
  171.         fs = GUI.FileSelector()
  172.         fs.activate(callback, fs)
  173.